package com.distributor.rest;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.distributor.common.ReturnObject;

/**
 * ${DESCRIPTION}
 */
public class BaseController<Service extends IService<T>, T> {

    @Autowired
    protected Service baseService;

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ReturnObject add(T entity) {
        baseService.insert(entity);
        return new ReturnObject(0, "添加成功", entity);
    }

    /**
     * 获取
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ReturnObject get(@PathVariable Long id) {
        return new ReturnObject(0, "获取成功", baseService.selectById(id));
    }

    /**
     * 更新
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public ReturnObject update(T entity) {
        baseService.updateById(entity);
        return new ReturnObject(0, "更新成功", entity);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ReturnObject remove(@PathVariable Long id) {
        baseService.deleteById(id);
        return new ReturnObject(0, "删除成功");
    }

    /**
     * 获取全部
     *
     * @return
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public ReturnObject list() {

        return new ReturnObject(0, "获取成功", baseService.selectList(new EntityWrapper().orderBy("crt_time",
                false)));
    }


}
